﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
/// <summary>
/// Summary description for UserService
/// </summary>
public class UserService
{
    #region CheckUseExits(String userName)
    /// <summary>
    /// Check User exist?
    /// </summary>
    /// <param name="active"></param>
    /// <returns></returns>
    public static bool CheckUseExits(String userName)
    {
        bool isExits = false;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                var users = from u in context.Users
                            join uTRN in context.UserRoles
                            on u.UserID equals uTRN.Id
                            where u.UserName == userName
                            select u;
                if (users != null && users.Count<User>() > 0)
                    isExits = true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
            }
            return isExits;
        }
    }
    #endregion

    #region Register(User user)
    public static bool Register(User user)
    {
        bool isSuccess;
        using (DatabaseDataContext context = DataFactory.GetDataContext())
        {
            try
            {
                UserRole _UserRole = new UserRole();
                _UserRole.Active = true;
                _UserRole.RoleId = 4;
                _UserRole.User = user;
                context.UserRoles.InsertOnSubmit(_UserRole);
                context.SubmitChanges();
                isSuccess = true;
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
                isSuccess = false;

            }
            return isSuccess;
        }

    }
    #endregion
}